--- title: Capture keywords: fastai sidebar: home_sidebar summary: "Helper " description: "Helper " nb_path: "01_capture.ipynb" ---
{% raw %}
/Users/eway/.pyenv/versions/3.8.3/lib/python3.8/site-packages/pandas/compat/__init__.py:97: UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.
  warnings.warn(msg)
/Users/eway/.pyenv/versions/3.8.3/lib/python3.8/site-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.7) or chardet (3.0.4) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
{% endraw %} {% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}

class OpenHSI[source]

OpenHSI(n_lines:int=16, processing_lvl:int=2, json_path:str='assets/cam_settings.json', pkl_path:str='assets/cam_calibration.pkl', print_settings=False) :: DataCube

Base Class for the OpenHSI Camera.

{% endraw %} {% raw %}
{% endraw %} {% raw %}
cam = OpenHSI()
{% endraw %}

Running in notebook slow downs.

{% raw %}

OpenHSI.collect[source]

OpenHSI.collect()

Collect the hyperspectral datacube.

{% endraw %}

To add a custom camera, five methods need to be defined in a class to:

  1. Initialise camera __init__,
  2. Open camera start_cam,
  3. Close camera stop_cam, and
  4. Capture a picture as a numpy array get_img.
  5. Update the exposure settings set_exposure

By inheriting from the OpenHSI class, all the methods to load settings/calibration files, collect datacube, saving data to NetCDF, and viewing as RGB are integrated. Furthermore, the custom camera class can be passed to a SettingsBuilder class for calibration.

{% raw %}

class SimulatedCamera[source]

SimulatedCamera(img_path:str=None, n_lines:int=16, processing_lvl:int=2, json_path:str='assets/cam_settings.json', pkl_path:str='assets/cam_calibration.pkl', print_settings=False) :: OpenHSI

Add

{% endraw %} {% raw %}
{% endraw %} {% raw %}
with SimulatedCamera(img_path="assets/great_hall_slide.png", n_lines=1028, processing_lvl = 3, json_path="assets/cam_settings.json",pkl_path="assets/cam_calibration.pkl") as cam:
    cam.collect()
    fig = cam.show(hist_eq=True)
    
fig
100%|██████████| 1028/1028 [00:17<00:00, 58.00it/s]
{% endraw %} {% raw %}
plt.imshow(cam.rgb_buff.data)
<matplotlib.image.AxesImage at 0x125f75340>
{% endraw %} {% raw %}
plt.plot(cam.λs,cam.xs[0],'r',label="X(λ)")
plt.plot(cam.λs,cam.ys[0],'g',label="Y(λ)")
plt.plot(cam.λs,cam.zs[0],'b',label="Z(λ)")
plt.grid(); plt.minorticks_on()
plt.legend()
plt.xlabel("λ (nm)")
plt.ylabel("CIE XYZ value")
{% endraw %} {% raw %}
plt.plot(cam.binned_wavelengths,cam.dc.data[350,300,:],label="rock")
plt.plot(cam.binned_wavelengths,cam.dc.data[50,300,:], label="sky")
plt.plot(cam.binned_wavelengths,cam.dc.data[150,300,:],label="water")
plt.legend()
plt.xlabel("wavelength (nm)")
plt.ylabel("pseudo-spectra")
{% endraw %} {% raw %}

class ProcessDatacube[source]

ProcessDatacube(fname:str, n_lines:int=16, processing_lvl:int=2, json_path:str='assets/cam_settings.json', pkl_path:str='assets/cam_calibration.pkl', print_settings=False) :: OpenHSI

Add

{% endraw %} {% raw %}
{% endraw %}

Below is still experimental stuff

{% raw %}
with ProcessDatacube(fname = "../load_multiple/2021-05-26 03_26_26.011211.nc",n_lines=3072,processing_lvl=-1) as cam:
    cam.collect()
    fig = cam.show(robust=True)
    
fig
  0%|          | 0/3072 [00:00<?, ?it/s]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-c433f48491e5> in <module>
      1 with ProcessDatacube(fname = "../load_multiple/2021-05-26 03_26_26.011211.nc",n_lines=3072,processing_lvl=-1) as cam:
----> 2     cam.collect()
      3     fig = cam.show(robust=True)
      4 
      5 fig

<ipython-input-8-b1e53126560f> in collect(self)
     23         self.start_cam()
     24         for i in tqdm(range(self.n_lines)):
---> 25             self.put(self.get_img())
     26 
     27             if callable(getattr(self,"get_temp",None)):

~/Desktop/openhsi/openhsi/data.py in put(self, x)
    376         """Applies the composed tranforms and writes the 2D array into the data cube. Stores a timestamp for each push."""
    377         self.timestamps.update()
--> 378         self.dc.put( self.pipeline(x) )
    379 
    380 

~/Desktop/openhsi/openhsi/data.py in put(self, line)
     73     def put(self, line:np.ndarray):
     74         """Writes a (n-1)darray into the buffer"""
---> 75         self.data[tuple(self.write_pos)] = line
     76 
     77         # if buffer full, update read position to keep track of oldest slot

ValueError: could not broadcast input array from shape (452,108) into shape (772,2064)
{% endraw %} {% raw %}
x = np.zeros((3, 4, 5))
x = np.moveaxis(x, -1, 0)
np.moveaxis(x, 0, -1).shape
(3, 4, 5)
{% endraw %}